package com.example.gameex2;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.WindowManager;

public class SpriteAnimation extends GraphicObject {
	private Rect m_Rect;
	private int m_fps;
	private int m_iFrames;
	
	private int m_CurrentFrame;
	private int m_SpriteWidth;
	private int m_SpriteHeight;
	
	private long m_FrameTimer;
	
	public SpriteAnimation(Bitmap bitmap) {
		super(bitmap);
		
	
		m_Rect = new Rect(0,0,0,0);
		m_FrameTimer = 0;
		m_CurrentFrame = 0;
	}

	public void InitSpriteData(int left, int top, int _width, int _height, int _fps, int iFrame)
	{
		m_SpriteWidth = _width;
		m_SpriteHeight = _height;
		m_Rect.top = top;
		m_Rect.bottom = m_Rect.top + m_SpriteHeight;
		m_Rect.left = left;
		m_Rect.right = m_Rect.left + m_SpriteWidth;
		m_fps = 1000 / _fps;
		m_iFrames = iFrame;
	}
	
	public void Draw(Canvas canvas)
	{
		Rect dest = new Rect(GetX(), GetY(), GetX() + m_SpriteWidth, GetY() + m_SpriteHeight);
		canvas.drawBitmap(GetBitmap(), m_Rect, dest, null);
	}
	
	public void DrawRotate(Canvas canvas, double degree)
	{
		canvas.save();
		canvas.rotate((float) degree, GetX() + m_SpriteWidth / 2, GetY() + m_SpriteHeight / 2);
		Rect dest = new Rect(GetX(), GetY(), GetX() + m_SpriteWidth, GetY() + m_SpriteHeight);
		canvas.drawBitmap(GetBitmap(), m_Rect, dest, null);
		canvas.restore();
	}	
	
	public void Update(long GameTime)
	{
		if(GameTime > m_FrameTimer + m_fps)
		{
			m_FrameTimer = GameTime;
			m_CurrentFrame += 1;
			if(m_CurrentFrame >= m_iFrames)
			{
				m_CurrentFrame = 0;
			}
		}
		m_Rect.left = m_CurrentFrame * m_SpriteWidth;
		m_Rect.right = m_Rect.left + m_SpriteWidth;
	}
	
	public int GetCenterX()
	{
		return GetX() + m_SpriteWidth / 2;
	}
	
	public int GetCenterY()
	{
		return GetY() + m_SpriteHeight / 2;
	}
}
